package kgc.LiuMang.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import kgc.LiuMang.entity.Orderproduct;
import kgc.LiuMang.entity.Ordertable;
import kgc.LiuMang.service.OrdertableService;

@Controller
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private OrdertableService os;
									//orderid,productid,pnumber,poneprice,ptotalprice
	@PostMapping("/addorder.do")  //ouserid,ostates,ofid,oname,ouaddress,outel,ouname,ototalprice
	@ResponseBody
	public int addOrder(@RequestBody JSONObject order) {
		System.out.println(order.toJSONString());
//		int succ=0;
//		int addO =  os.addOrder(order);
//		if(addO>0) {
//			System.out.println("添加订单成功");
//			succ=1;
//		}
//		for (Orderproduct orderproduct : oplist) {
//				if(os.addOrderPro(orderproduct)>0) {
//					System.out.println("插入商品成功");
//					succ+=1;
//				}else {
//					System.out.println("插入商品失败");
//				}
//				
//		}
//		return succ;
		return 1;
	}
	
	@PostMapping("/addorder2.do")  //ouserid,ostates,ofid,oname,ouaddress,outel,ouname,ototalprice
	@ResponseBody
	public int addOrder2(@RequestBody JSONObject data) {
		String data1 = JSON.toJSONString(data);
		JSONObject json = JSON.parseObject(data1);
		Ordertable order1 = json.getObject("order", Ordertable.class);
		List<Orderproduct> list1 =  order1.getOplist();
		System.out.println(order1+"******************");
		order1.getOfid();
		System.out.println(list1+"====================");
		int succ=0;
		if(os.addOrder(order1)>0) {
			System.out.println("添加订单成功");
			succ=1;
			if(order1.getOstates()!=6) {
				if(os.updateUpFTurn(order1.getOfid(), order1.getOtotalprice())>0) {
					System.out.println("果销点营业额添加成功");
				}
			}else if(order1.getOstates()==6) {
				if(os.updateDownFTurn(order1.getOfid(), order1.getOtotalprice())>0) {
					System.out.println("果销点营业减少加成功");
				}
			}
		}
		for (Orderproduct orderproduct : list1) {
				if(os.addOrderPro(orderproduct)>0) {
					System.out.println("插入商品成功");
					succ+=1;
				}else {
					System.out.println("插入商品失败");
				}
				
		}
		return succ;
	}
	
	
	@RequestMapping("/update.do")
	@ResponseBody
	public int updateOrder(@RequestParam Integer oid,@RequestParam Integer ostates) {
		System.out.println(oid+"============="+ostates);
		int success =0;
		if(os.updateOrderStates(oid, ostates)>0) {
			System.out.println("订单状态修改成功");
			success=1;
		}
		return success;
	}
	
	@RequestMapping("/getOrder.do")
	public List<Ordertable> getOrderLimit(@RequestParam(required = false) Integer pageNum,
							@RequestParam(required = false) Integer pageSize,
							@RequestParam(required = false) Integer ouserid,
							@RequestParam(required = false) Integer ofid,
							@RequestParam(required = false) Integer ostates){
		System.out.println("pageNum:"+pageNum+"pageSize:"+pageSize+"ouserid:"+ouserid+"ofid:"+ofid+"ostates:"+ostates);
		Integer pageNum1=0;
		Integer pageSize1=10;
		Integer ouserid1=null;
		Integer ofid1=null;
		Integer ostates1=null;
		if(pageNum!=null) {
			pageNum1=(pageNum-1)*pageSize;
		}
		if(ofid!=null) {
			ofid1=ofid;
		}
		if(ostates!=null) {
			ostates1=ostates;
		}
		List<Ordertable> list = os.getOrderLimit(pageNum1, pageSize1, ouserid1, ofid1, ostates1);
		System.out.println(list);
		return list;
	}
	
	
	
	@RequestMapping("test.do")
	@ResponseBody
	public String test() {
		String test="测试是否联通";
		return test;
	}
}
